package com.fhj.bookshop.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhj.bookshop.annotation.LoginToken;
import com.fhj.bookshop.annotation.PassToken;
import com.fhj.bookshop.common.R;
import com.fhj.bookshop.entity.Product;
import com.fhj.bookshop.mapper.ProductMapper;
import com.fhj.bookshop.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fu
 * @since 2023-12-12
 */

@RestController
public class ProductController {

    @Autowired
    private IProductService productService;

    @Autowired
    private ProductMapper productMapper;

    @PassToken
    @RequestMapping(value = "/product/{pageCode}/{pageSize},{type}",method = RequestMethod.GET)
    public R Product(@PathVariable(value = "pageCode") int pageCode,@PathVariable(value="pageSize") int pageSize,@PathVariable String type){
        System.out.println(pageCode+"...."+pageSize);
        Page<Product> pageInfo =productService.productPageService(pageCode,pageSize,type);
        return R.ok("商品列表展示成功").setData(pageInfo);
    }

    @LoginToken
    @RequestMapping(value = "/goods/{proId}",method = RequestMethod.GET)
    public R Goods(@PathVariable Integer proId){
         Product product=productService.findProductById(proId);
         return R.ok("商品信息载入成功").setData(product);
    }

    @PassToken
    @RequestMapping(value ="/findType",method = RequestMethod.GET)
    public R findType(){
        return R.ok("查找分类成功").setData(productMapper.selectDistinctType());
    }

    @LoginToken
    @RequestMapping(value = "/adminProduct/{pageCode}/{pageSize}",method = RequestMethod.GET)
    public R AdminProduct(@PathVariable Integer pageCode,@PathVariable Integer pageSize){
        Page<Product> pageInfo=productService.AdminProduct(pageCode,pageSize);
        return R.ok("admin商品列表展示成功").setData(pageInfo);
    }

    @LoginToken
    @RequestMapping(value = "/addProduct",method = RequestMethod.POST)
    public R addProduct(@RequestBody Product product){
        productMapper.insert(product);
        return R.ok("成功增加商品").setData(product);
    }

    @LoginToken
    @RequestMapping(value = "/changeProduct",method = RequestMethod.POST)
    public R changeProduct(@RequestBody Product product){
        productMapper.updateById(product);
        return R.ok("成功修改商品").setData(product);
    }
}
